package cn.edu.nju.askinginhand.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nju.askinginhand.util.JSONParser;

/**
 * 
 * @author ZhangZu
 * 
 */
public class PersonalInfoActivity extends Activity {

	private EditText nameEdit;
	private EditText ageEdit;
	private EditText emailEdit;
	private EditText phoneEdit;
	private Button saveBtn;
	private Button cancelBtn;
	private String name;
	private String age;
	private String gender;
	private String email;
	private String phone;
	
	private Spinner genderSpinner;
	private ArrayAdapter<String> adapter;
	
	private static final String[] myGender = {"其他","男性","女性"};
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View contentView = LayoutInflater.from(this.getParent()).inflate(R.layout.personal_info_check, null);
		setContentView(contentView);
		nameEdit = (EditText) findViewById(R.id.nameEdit);
		ageEdit = (EditText) findViewById(R.id.ageEdit);
		emailEdit = (EditText) findViewById(R.id.emailEdit);
		phoneEdit = (EditText) findViewById(R.id.phoneEdit);
		saveBtn = (Button) findViewById(R.id.saveBtn);
		cancelBtn = (Button) findViewById(R.id.cancelBtn);
		
		genderSpinner=(Spinner)findViewById(R.id.genderSpinner);
		
		adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,myGender);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		genderSpinner.setAdapter(adapter);
		
		genderSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				gender=myGender[arg2];
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		initButtons();
		initInfo();
	}

	private void initInfo() {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", LoginActivity.uid));
		String raw = JSONParser.getOut("UserInfoRetrieveAction"+LoginActivity.filter, params);
		System.out.println("raw=" + raw);
		String userinfoStr = JSONParser.removeRoot(raw, "userinfo");
		if (userinfoStr != null) {
			Map<String, String> userinfo = JSONParser.getMap(userinfoStr);
			nameEdit.setText(userinfo.get("name").toString(),
					TextView.BufferType.EDITABLE);
			ageEdit.setText(userinfo.get("age").toString(),
					TextView.BufferType.EDITABLE);
			
			gender=userinfo.get("gender").toString();
			int n= checkGender(gender);
			genderSpinner.setSelection(n);

			emailEdit.setText(userinfo.get("email").toString(),
					TextView.BufferType.EDITABLE);
			phoneEdit.setText(userinfo.get("phone").toString(),
					TextView.BufferType.EDITABLE);
		}

		/*
		 * name=nameEdit.setText(); age=ageEdit.getText();
		 * gender=genderEdit.setText(); email=emailEdit.setText();
		 * phone=phoneEdit.setText();
		 */
	}

	private void initButtons() {
		saveBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				name = nameEdit.getText().toString();
				age = ageEdit.getText().toString();
				email = emailEdit.getText().toString();
				phone = phoneEdit.getText().toString();
				boolean bool = checkInput();
				if(bool){
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					System.out.println(LoginActivity.uid);
					params.add(new BasicNameValuePair("uid", LoginActivity.uid));
					params.add(new BasicNameValuePair("name", name));
					params.add(new BasicNameValuePair("age", age));
					params.add(new BasicNameValuePair("gender", gender));
					params.add(new BasicNameValuePair("email", email));
					params.add(new BasicNameValuePair("phone", phone));
					String raw = JSONParser.getOut("UserInfoSaveAction"+LoginActivity.filter, params);
					String result = JSONParser.getStringData(raw);
					if (result.equals("success")) {
						Toast.makeText(PersonalInfoActivity.this, "修改成功",
								Toast.LENGTH_LONG).show();
					}else {
						Toast.makeText(PersonalInfoActivity.this, "修改失败",
								Toast.LENGTH_LONG).show();
					}	
				}else{
					Toast.makeText(PersonalInfoActivity.this, "输入格式错误",
							Toast.LENGTH_LONG).show();
				}
				
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(PersonalInfoActivity.this,MainTab.class));	
			}
			
			
		});
		
	}

	/**
	 * 检查输入是否合法,有待完善。
	 * 
	 * @return
	 */
	private boolean checkInput() {
		return true;

	}
	/**
	 * 对输入的性别做检查
	 */
	private int checkGender(String Gender){
		int n=0;
        while(!gender.trim().equals(myGender[n])){
        	n++;
        }
		return n;		
	}
	

}
